package cn.hbu.zlfw.admin.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.hbu.zlfw.admin.entity.UnitAdmin;
import cn.hbu.zlfw.admin.service.UnitAdminService;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.DicSubjectUtil;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.dic.entity.DicPatentState;
import cn.hbu.zlfw.dic.entity.DicSchool;
import cn.hbu.zlfw.dic.entity.DicSubject;
import cn.hbu.zlfw.dic.entity.DicUserType;
import cn.hbu.zlfw.dic.service.DicPatentStateService;
import cn.hbu.zlfw.dic.service.DicSchoolService;
import cn.hbu.zlfw.dic.service.DicSubjectService;
import cn.hbu.zlfw.dic.service.DicUserTypeService;
import cn.hbu.zlfw.research.entity.Researcher;
import cn.hbu.zlfw.research.service.ResearcherService;

/**
 * @ClassName: RootAdminController 
 * @Description: 总管理员控制层
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月28日 下午10:27:34 
 * @vsersion:v1.0
 */
@Controller
@RequestMapping("/rootAdmin")
public class RootAdminController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(RootAdminController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	/**
	 * 注入service
	 */
	@Autowired
	private UnitAdminService unitAdminService;
	@Autowired
	private DicSubjectService dicSubjectService;
	@Autowired
	private DicSchoolService dicSchoolService;
	@Autowired
	private ResearcherService researcherService;
	@Autowired
	private DicUserTypeService dicUserTypeService;
	@Autowired
	private DicPatentStateService dicPatentStateService;
	/**
	 * 
	  * @Title: toMain 
	  * @Description: 转向后台管理界面
	  * @return String 
	  * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="总管理员登录成功,转向后台管理界面")
	@RequestMapping("/tomain.do")
	public String toMain() {
		return "admin/main";
	}
	
	/**
	 *  总管理员管理所有单位管理员信息
	 */
	/**
	 * @Title: listUnitAdminByParams 
	 * @Description: 总管理员查询所有单位管理员信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="总管理员查询所有单位管理员信息")
	@RequestMapping("/listUnitAdminByParams.do")
	public String listUnitAdminByParams(@ModelAttribute("pageModel")PageModel<UnitAdmin> pageModel,
			@RequestParam(value="userName",required=false)String userName,
			@RequestParam(value="schoolType",required=false)Integer schoolType,ModelMap map) {
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);
		}
		pageModel = this.unitAdminService.listUniAdminByParamsToRoot(pageModel,userName,schoolType);
		pageModel.setUrl("/rootAdmin/listUnitAdminByParams.do");
		List<UnitAdmin> unitAdmins = pageModel.getList();
		if(unitAdmins != null && !unitAdmins.isEmpty()) {
			//解析学科信息
			map.put("unitAdminChars", DicSubjectUtil.parseSubjectByUnitAdmins(unitAdmins,dicSubjectService));
		}
		map.put("dicSchools", this.dicSchoolService.findAll());
		map.put("userName", userName);
		map.put("schoolType", schoolType);
		if(userName!=null&&userName!="")
			map.put("userName", userName);
		if(schoolType!=null&&schoolType!=0)
			map.put("schoolType", schoolType);
		return "admin/root/unitlist";
	}
	
	/**
	 * @Title: delUnitAdminById 
	 * @Description: 总管理员删除单位管理员信息:采用假删除
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.DELETE,description="总管理员删除单位管理员信息")
	@RequestMapping("/delUnitAdminById.do")
	public String delUnitAdminById(@RequestParam(value="unitId",required=false)String unitId) {
		UnitAdmin unitAdmin = this.unitAdminService.getById(unitId);
		unitAdmin.setDelState(1);
		unitAdmin.setUpdateTime(new Date());
		this.unitAdminService.update(unitAdmin);
		return "forward:/rootAdmin/listUnitAdminByParams.do";
	}
	
	/**
	 * @Title: updateUnitAdminUI 
	 * @Description: 转向修改单位管理员信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向修改单位管理员信息页面")
	@RequestMapping("/updateUnitAdminUI.do")
	public String updateUnitAdminUI(ModelMap map,@RequestParam(value="unitId",required=false)String unitId) {
		HttpSession session = request.getSession();
		if(unitId != null && unitId != "") {
			session.setAttribute("sesssionRootUnitId", unitId);
		}
		map.put("dicSubjects", this.dicSubjectService.findAll());
		map.put("dicSchools", this.dicSchoolService.findAll());
		UnitAdmin unitAdmin = this.unitAdminService
				.getById(session.getAttribute("sesssionRootUnitId").toString());
		if(unitId != null && unitId != "") {
			map.put("unitAdmin", unitAdmin);
		}
		return "admin/root/updateunit";
	}
	/**
	 * 
	 * @Title: updateUnitAdmin
	 * @Description: 总管理员更新单位管理员
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.UPDATE,description="总管理员更新单位管理员")
	@RequestMapping("updateUnitAdmin.do")
	public String updateScienceExpert(@Valid @ModelAttribute("unitAdmin")UnitAdmin unitAdmin,
			BindingResult bindingResult,ModelMap map) {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/updateUnitAdminUI.do";
		}
		//检验手机号是否已经被其他人注册注册（全部不区分单位）
		UnitAdmin unitAdmin2 = this.unitAdminService
				.getUnitAdminToUpdate(unitAdmin.getPhoneNumber(),unitAdmin.getId());
		if(unitAdmin2 != null) {
			map.put("phoneError", "该手机号已经被其他人注册");
			return "forward:/rootAdmin/updateUnitAdminUI.do";
		}
		//更新数据
		UnitAdmin unitAdmin3 = this.unitAdminService.getById(unitAdmin.getId());
		unitAdmin3.setSchoolType(unitAdmin.getSchoolType());
		unitAdmin3.setSubjectTypes(unitAdmin.getSubjectTypes());
		unitAdmin3.setUserName(unitAdmin.getUserName());
		unitAdmin3.setPassword(unitAdmin.getPassword());
		unitAdmin3.setDepartment(unitAdmin.getDepartment());
		unitAdmin3.setPhoneNumber(unitAdmin.getPhoneNumber());
		unitAdmin3.setQQ(unitAdmin.getQQ());
		unitAdmin3.setWeChat(unitAdmin.getWeChat());
		//更新修改时间
		unitAdmin3.setUpdateTime(new Date());
		this.unitAdminService.update(unitAdmin3);
		return "redirect:/rootAdmin/listUnitAdminByParams.do";
	}
	/**
	 * 
	 * @Title: saveUnitAdminUI 
	 * @Description: 转向添加单位管理员信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向添加单位管理员信息页面")
	@RequestMapping("/saveUnitAdminUI.do")
	public String saveUnitAdminUI(ModelMap map) {
		map.put("dicSubjects", this.dicSubjectService.findAll());
		map.put("dicSchools", this.dicSchoolService.findAll());
		return "admin/root/addunit";
	}
	/**
	 * @throws Exception 
	 * @Title: saveUnitAdmin
	 * @Description: 总管理员保存单位管理员信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="总管理员保存单位管理员信息")
	@RequestMapping("/saveUnitAdmin.do")
	public String saveUnitAdmin(@Valid @ModelAttribute("unitAdmin") UnitAdmin unitAdmin,
			BindingResult bindingResult,ModelMap map) throws Exception {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/saveUnitAdminUI.do";
		}
		//*检验手机号是否已经注册（全部不区分单位）
		UnitAdmin unitAdmin2 = this.unitAdminService.getUnitAdminByParams(unitAdmin.getPhoneNumber());
		if(unitAdmin2 != null) {
			map.put("phoneError", "该手机号已经注册");
			return "forward:/rootAdmin/saveUnitAdminUI.do";
		}
		unitAdmin.setUserType(DicUserType.UNIT_ADMIN_USER);
		unitAdmin.setCreateTime(new Date());
		unitAdmin.setUpdateTime(new Date());
		unitAdmin.setDelState(0);
		this.unitAdminService.save(unitAdmin);
		return "redirect:/rootAdmin/listUnitAdminByParams.do";
	}
	
	/**
	 * 总管理员管理科研人员信息
	 */
	/**
	 * @Title: listResearcherByParams 
	 * @Description: 总管理员查询所有科研人员信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="总管理员查询所有科研人员信息")
	@RequestMapping("/listResearcherByParams.do")
	public String listResearcherByParams(@ModelAttribute("pageModel")PageModel<Researcher> pageModel,
			@RequestParam(value="userName",required=false)String userName,
			@RequestParam(value="schoolType",required=false)Integer schoolType,ModelMap map) {
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);                         
		}
		pageModel = this.researcherService.listResearcherByParamsToRoot(pageModel,userName,schoolType);
		pageModel.setUrl("/rootAdmin/listResearcherByParams.do");
		map.put("dicSchools", this.dicSchoolService.findAll());
		if(userName!=null&&userName!="")
			map.put("userName", userName);
		if(schoolType!=null&&schoolType!=0)
			map.put("schoolType", schoolType);
		return "admin/root/researcherlist";
	}
	
	/**
	 * @Title: delResearcherById 
	 * @Description: 总管理员删除科研人员信息:采用假删除
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.DELETE,description="总管理员删除科研人员信息")
	@RequestMapping("/delResearcherById.do")
	public String delResearcherById(@RequestParam(value="researcherId",required=false)String researcherId) {
		Researcher researcher = this.researcherService.getById(researcherId);
		researcher.setDelState(1);
		researcher.setUpdateTime(new Date());
		this.researcherService.update(researcher);
		return "forward:/rootAdmin/listResearcherByParams.do";
	}
	
	/**
	 * @Title: updateResearcherUI 
	 * @Description: 转向修改科研人员信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向修改科研人员信息页面")
	@RequestMapping("/updateResearcherUI.do")
	public String updateResearcherUI(ModelMap map,@RequestParam(value="researcherId",required=false)String researcherId) {
		HttpSession session = request.getSession();
		if(researcherId != null && researcherId != "") {
			session.setAttribute("sesssionRootResearcherId", researcherId);
		}
		map.put("dicSchools", this.dicSchoolService.findAll());
		Researcher researcher = 
				this.researcherService.getById(session.getAttribute("sesssionRootResearcherId").toString());
		if(researcherId != null && researcherId != "") {
			map.put("researcher", researcher);
		}
		return "admin/root/updateresearcher";
	}
	/**
	 * 
	 * @Title: updateResearcher
	 * @Description: 总管理员更新科研人员
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.UPDATE,description="总管理员更新科研人员")
	@RequestMapping("updateResearcher.do")
	public String updateResearcher(@Valid @ModelAttribute("researcher")Researcher researcher,
			BindingResult bindingResult,ModelMap map) {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/updateResearcherUI.do";
		}
		//检验手机号是否已经被其他人注册（全部不区分单位）
		Researcher researcher2 = this.researcherService
				.getResearcherToUpdate(researcher.getPhoneNumber(),researcher.getId());
		if(researcher2 != null) {
			map.put("phoneError", "该手机号已经被其他人注册");
			return "forward:/rootAdmin/updateResearcherUI.do";
		}
		//更新数据
		Researcher researcher3 = this.researcherService.getById(researcher.getId());
		researcher3.setSchoolType(researcher.getSchoolType());
		researcher3.setUserName(researcher.getUserName());
		researcher3.setPassword(researcher.getPassword());
		researcher3.setDepartment(researcher.getDepartment());
		researcher3.setPhoneNumber(researcher.getPhoneNumber());
		researcher3.setResearch(researcher.getResearch());
		researcher3.setEmail(researcher.getEmail());
		researcher3.setQQ(researcher.getQQ());
		researcher3.setWeChat(researcher.getWeChat());
		//更新修改时间
		researcher3.setUpdateTime(new Date());
		this.researcherService.update(researcher3);
		return "redirect:/rootAdmin/listResearcherByParams.do";
	}
	/**
	 * 
	 * @Title: saveResearcherUI 
	 * @Description: 转向添加科研人员信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向添加科研人员信息页面")
	@RequestMapping("/saveResearcherUI.do")
	public String saveResearcherUI(ModelMap map) {
		map.put("dicSchools", this.dicSchoolService.findAll());
		return "admin/root/addresearcher";
	}
	/**
	 * @throws Exception 
	 * @Title: saveResearcher
	 * @Description: 总管理员保存科研人员信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="总管理员保存科研人员信息")
	@RequestMapping("/saveResearcher.do")
	public String saveResearcher(@Valid @ModelAttribute("researcher")Researcher researcher,
			BindingResult bindingResult,ModelMap map) throws Exception {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/saveResearcherUI.do";
		}
		//*检验手机号是否已经注册（全部不区分单位）
		Researcher researcher2 = this.researcherService.findResearcherByPhone(researcher.getPhoneNumber());
		if(researcher2 != null) {
			map.put("phoneError", "该手机号已经注册");
			return "forward:/rootAdmin/saveResearcherUI.do";
		}
		researcher.setUserType(DicUserType.RESEARCHER_USER);
		researcher.setCreateTime(new Date());
		researcher.setUpdateTime(new Date());
		researcher.setDelState(0);
		this.researcherService.save(researcher);
		return "redirect:/rootAdmin/listResearcherByParams.do";
	}
	
	/**
	 * 总管理员管理角色类型信息
	 */
	/**
	 * @Title: listUserTypes 
	 * @Description: 总管理员查询所有角色类型信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="总管理员查询所有角色类型信息")
	@RequestMapping("/listUserTypes.do")
	public String listUserTypes(@ModelAttribute("pageModel")PageModel<DicUserType> pageModel,ModelMap map) {
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);                         
		}
		pageModel = this.dicUserTypeService.listUserTypesToRoot(pageModel);
		pageModel.setUrl("/rootAdmin/listUserTypes.do");
		return "admin/root/dicpage/usertypelist";
	}
	
	/**
	 * @Title: updateUserTypeUI 
	 * @Description: 转向修改角色类型信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向修改角色类型信息页面")
	@RequestMapping("/updateUserTypeUI.do")
	public String updateUserTypeUI(@RequestParam(value="userTypeId",required=false)String userTypeId,
			ModelMap map) {
		HttpSession session = request.getSession();
		if(userTypeId != null && userTypeId != "") {
			session.setAttribute("sesssionRootUserTypeId", userTypeId);
		}
		DicUserType userType = 
				this.dicUserTypeService.getById(session.getAttribute("sesssionRootUserTypeId").toString());
		if(userTypeId != null && userTypeId != "") {
			map.put("userType", userType);
		}
		return "admin/root/dicpage/updateusertype";
	}
	/**
	 * 
	 * @Title: updateUserType
	 * @Description: 总管理员更新角色类型
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.UPDATE,description="总管理员更新角色类型")
	@RequestMapping("updateUserType.do")
	public String updateUserType(@Valid @ModelAttribute("userType")DicUserType userType,
			BindingResult bindingResult,ModelMap map) {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/updateUserTypeUI.do";
		}
		//更新数据
		DicUserType userType2 = this.dicUserTypeService.getById(userType.getId());
		userType2.setDescription(userType.getDescription());
		userType2.setRemarks(userType.getRemarks());
		//更新修改时间
		userType2.setUpdateTime(new Date());
		this.dicUserTypeService.update(userType2);
		return "redirect:/rootAdmin/listUserTypes.do";
	}
	/**
	 * 
	 * @Title: saveUserTypeUI 
	 * @Description: 转向添加角色类型信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向添加角色类型信息页面")
	@RequestMapping("/saveUserTypeUI.do")
	public String saveUserTypeUI(ModelMap map) {
		return "admin/root/dicpage/addusertype";
	}
	/**
	 * @throws Exception 
	 * @Title: saveUserType
	 * @Description: 总管理员保存角色类型信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="总管理员保存角色类型信息")
	@RequestMapping("/saveUserType.do")
	public String saveUserType(@Valid @ModelAttribute("userType")DicUserType userType,
			BindingResult bindingResult,ModelMap map) throws Exception {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/saveUserTypeUI.do";
		}
		//保存信息
		userType.setCreateTime(new Date());
		userType.setUpdateTime(new Date());
		userType.setDelState(0);
		this.dicUserTypeService.save(userType);
		return "redirect:/rootAdmin/listUserTypes.do";
	}
	
	/**
	 * 总管理员管理学科分类信息
	 */
	/**
	 * @Title: listSubjects 
	 * @Description: 总管理员查询所有学科分类信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="总管理员查询所有学科分类信息")
	@RequestMapping("/listSubjects.do")
	public String listSubjects(@ModelAttribute("pageModel")PageModel<DicSubject> pageModel,ModelMap map) {
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);                         
		}
		pageModel = this.dicSubjectService.listSubjectsToRoot(pageModel);
		pageModel.setUrl("/rootAdmin/listSubjects.do");
		return "admin/root/dicpage/subjectlist";
	}
	
	/**
	 * @Title: updateSubjectUI 
	 * @Description: 转向修改学科分类信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向修改学科分类信息页面")
	@RequestMapping("/updateSubjectUI.do")
	public String updateSubjectUI(@RequestParam(value="subjectId",required=false)String subjectId,
			ModelMap map) {
		HttpSession session = request.getSession();
		if(subjectId != null && subjectId != "") {
			session.setAttribute("sesssionRootSubjectId", subjectId);
		}
		DicSubject subject = 
				this.dicSubjectService.getById(session.getAttribute("sesssionRootSubjectId").toString());
		if(subjectId != null && subjectId != "") {
			map.put("subject", subject);
		}
		return "admin/root/dicpage/updatesubject";
	}
	/**
	 * 
	 * @Title: updateSubject
	 * @Description: 总管理员更新学科分类
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.UPDATE,description="总管理员更新学科分类")
	@RequestMapping("updateSubject.do")
	public String updateSubject(@Valid @ModelAttribute("subject")DicSubject subject,
			BindingResult bindingResult,ModelMap map) {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/updateSubjectUI.do";
		}
		//更新数据
		DicSubject subject2 = this.dicSubjectService.getById(subject.getId());
		subject2.setDescription(subject.getDescription());
		subject2.setRemarks(subject.getRemarks());
		//更新修改时间
		subject2.setUpdateTime(new Date());
		this.dicSubjectService.update(subject2);
		return "redirect:/rootAdmin/listSubjects.do";
	}
	/**
	 * 
	 * @Title: saveSubjectUI 
	 * @Description: 转向添加学科分类信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向添加学科分类信息页面")
	@RequestMapping("/saveSubjectUI.do")
	public String saveSubjectUI(ModelMap map) {
		return "admin/root/dicpage/addsubject";
	}
	/**
	 * @throws Exception 
	 * @Title: saveSubject
	 * @Description: 总管理员保存学科分类信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="总管理员保存学科分类信息")
	@RequestMapping("/saveSubject.do")
	public String saveSubject(@Valid @ModelAttribute("subject")DicSubject subject,
			BindingResult bindingResult,ModelMap map) throws Exception {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/saveSubjectUI.do";
		}
		//保存信息
		subject.setCreateTime(new Date());
		subject.setUpdateTime(new Date());
		subject.setDelState(0);
		this.dicSubjectService.save(subject);
		return "redirect:/rootAdmin/listSubjects.do";
	}
	
	/**
	 * 总管理员管理单位分类信息
	 */
	/**
	 * @Title: listSchools 
	 * @Description: 总管理员查询所有单位分类信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="总管理员查询所有单位分类信息")
	@RequestMapping("/listSchools.do")
	public String listSchools(@ModelAttribute("pageModel")PageModel<DicSchool> pageModel,ModelMap map) {
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);                         
		}
		pageModel = this.dicSchoolService.listSchoolsToRoot(pageModel);
		pageModel.setUrl("/rootAdmin/listSchools.do");
		return "admin/root/dicpage/schoollist";
	}
	
	/**
	 * @Title: updateSchoolUI 
	 * @Description: 转向修改单位分类信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向修改单位分类信息页面")
	@RequestMapping("/updateSchoolUI.do")
	public String updateSchoolUI(@RequestParam(value="schoolId",required=false)String schoolId,
			ModelMap map) {
		HttpSession session = request.getSession();
		if(schoolId != null && schoolId != "") {
			session.setAttribute("sesssionRootSchoolId", schoolId);
		}
		DicSchool school = 
				this.dicSchoolService.getById(session.getAttribute("sesssionRootSchoolId").toString());
		if(schoolId != null && schoolId != "") {
			map.put("school", school);
		}
		return "admin/root/dicpage/updateschool";
	}
	/**
	 * 
	 * @Title: updateSchool
	 * @Description: 总管理员更新单位分类
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.UPDATE,description="总管理员更新单位分类")
	@RequestMapping("updateSchool.do")
	public String updateSchool(@Valid @ModelAttribute("school")DicSchool school,
			BindingResult bindingResult,ModelMap map) {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/updateSchoolUI.do";
		}
		//更新数据
		DicSchool school2 = this.dicSchoolService.getById(school.getId());
		school2.setDescription(school.getDescription());
		school2.setRemarks(school.getRemarks());
		//更新修改时间
		school2.setUpdateTime(new Date());
		this.dicSchoolService.update(school2);
		return "redirect:/rootAdmin/listSchools.do";
	}
	/**
	 * 
	 * @Title: saveSchoolUI 
	 * @Description: 转向添加单位分类信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向添加单位分类信息页面")
	@RequestMapping("/saveSchoolUI.do")
	public String saveSchoolUI(ModelMap map) {
		return "admin/root/dicpage/addschool";
	}
	/**
	 * @throws Exception 
	 * @Title: saveSchool
	 * @Description: 总管理员保存单位分类信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="总管理员保存单位分类信息")
	@RequestMapping("/saveSchool.do")
	public String saveSchool(@Valid @ModelAttribute("school")DicSchool school,
			BindingResult bindingResult,ModelMap map) throws Exception {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/saveSchoolUI.do";
		}
		//保存信息
		school.setCreateTime(new Date());
		school.setUpdateTime(new Date());
		school.setDelState(0);
		this.dicSchoolService.save(school);
		return "redirect:/rootAdmin/listSchools.do";
	}
	
	/**
	 * 总管理员管理专利状态信息
	 */
	/**
	 * @Title: listPatentStates 
	 * @Description: 总管理员查询所有专利状态信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="总管理员查询所有专利状态信息")
	@RequestMapping("/listPatentStates.do")
	public String listPatentStates(@ModelAttribute("pageModel")PageModel<DicPatentState> pageModel,ModelMap map) {
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);                         
		}
		pageModel = this.dicPatentStateService.listPatentStatesToRoot(pageModel);
		pageModel.setUrl("/rootAdmin/listPatentStates.do");
		return "admin/root/dicpage/patentstatelist";
	}
	
	/**
	 * @Title: updatePatentStateUI 
	 * @Description: 转向修改专利状态信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向修改专利状态信息页面")
	@RequestMapping("/updatePatentStateUI.do")
	public String updatePatentStateUI(@RequestParam(value="patentStateId",required=false)String patentStateId,
			ModelMap map) {
		HttpSession session = request.getSession();
		if(patentStateId != null && patentStateId != "") {
			session.setAttribute("sesssionRootPatentStateId", patentStateId);
		}
		DicPatentState patentState = 
				this.dicPatentStateService.getById(session.getAttribute("sesssionRootPatentStateId").toString());
		if(patentStateId != null && patentStateId != "") {
			map.put("patentState", patentState);
		}
		return "admin/root/dicpage/updatepatentstate";
	}
	/**
	 * 
	 * @Title: updatePatentState
	 * @Description: 总管理员更新专利状态
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.UPDATE,description="总管理员更新专利状态")
	@RequestMapping("updatePatentState.do")
	public String updatePatentState(@Valid @ModelAttribute("patentState")DicPatentState patentState,
			BindingResult bindingResult,ModelMap map) {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/updatePatentStateUI.do";
		}
		//更新数据
		DicPatentState patentState2 = this.dicPatentStateService.getById(patentState.getId());
		patentState2.setDescription(patentState.getDescription());
		patentState2.setRemarks(patentState.getRemarks());
		//更新修改时间
		patentState2.setUpdateTime(new Date());
		this.dicPatentStateService.update(patentState2);
		return "redirect:/rootAdmin/listPatentStates.do";
	}
	/**
	 * 
	 * @Title: savePatentStateUI 
	 * @Description: 转向添加专利状态信息页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向添加专利状态信息页面")
	@RequestMapping("/savePatentStateUI.do")
	public String savePatentStateUI(ModelMap map) {
		return "admin/root/dicpage/addpatentstate";
	}
	/**
	 * @throws Exception 
	 * @Title: savePatentState
	 * @Description: 总管理员保存专利状态信息
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="总管理员保存专利状态信息")
	@RequestMapping("/savePatentState.do")
	public String savePatentState(@Valid @ModelAttribute("patentState")DicPatentState patentState,
			BindingResult bindingResult,ModelMap map) throws Exception {
		if(bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
            	logger.info("Code:" + fieldError.getCode() + ", object:"
                        + fieldError.getObjectName() + ", field:"
                        + fieldError.getField());
			}
			return "forward:/rootAdmin/savePatentStateUI.do";
		}
		//保存信息
		patentState.setCreateTime(new Date());
		patentState.setUpdateTime(new Date());
		patentState.setDelState(0);
		this.dicPatentStateService.save(patentState);
		return "redirect:/rootAdmin/listPatentStates.do";
	}
}
